home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 2000 #4 / Amiga Plus CD - 2000 - No. 4.iso / Tools / Misc / bgui / Examples / Source / StringHook.c < prev    next >
C/C++ Source or Header  |  2000-05-09  |  5KB  |  188 lines

  1. /*
  2.  * @(#) $Header: /cvsroot/bgui/examples/StringHook.c,v 41.11 2000/05/09 20:33:55 mlemos Exp $
  3.  *
  4.  * StringHook.c
  5.  *
  6.  * (C) Copyright 1998 Manuel Lemos.
  7.  * (C) Copyright 1995 Jaba Development.
  8.  * (C) Copyright 1995 Jan van den Baard.
  9.  * All Rights Reserved.
  10.  *
  11.  * $Log: StringHook.c,v $
  12.  * Revision 41.11  2000/05/09 20:33:55  mlemos
  13.  * Bumped to revision 41.11
  14.  *
  15.  * Revision 1.2  2000/05/09 19:59:10  mlemos
  16.  * Merged with the branch Manuel_Lemos_fixes.
  17.  *
  18.  * Revision 1.1.2.1  1998/02/28 17:45:42  mlemos
  19.  * Ian sources
  20.  *
  21.  *
  22.  */
  23.  
  24. /* Execute me to compile with DICE V3.0
  25. dcc StringHook.c -proto -mi -ms -mRR -lbgui
  26. quit
  27. */
  28.  
  29. #include "DemoCode.h"
  30.  
  31. #include <ctype.h>
  32.  
  33. /*
  34. **      Object ID's.
  35. **/
  36. #define ID_QUIT                 1
  37.  
  38. /*
  39. **      Info text.
  40. **/
  41. UBYTE  *WinInfo = ISEQ_C "This demo shows you how to include a\n"
  42.                   "string object edit hook. The string object\n"
  43.                   "has a edit hook installed which only allows\n"
  44.                   "you to enter hexadecimal characters. It will\n"
  45.                   "also convert the character you click on to 0.";
  46.  
  47. /*
  48. **      String object edit hook. Copied from the
  49. **      RKM-Manual Libraries. Page 162-166.
  50. **/
  51. SAVEDS ASM ULONG HexHookFunc( REG(a0) struct Hook *hook, REG(a2) struct SGWork *sgw, REG(a1) ULONG *msg )
  52. {
  53.         ULONG           rc = ~0;
  54.  
  55.         switch ( *msg ) {
  56.  
  57.                 case    SGH_KEY:
  58.                         /*
  59.                         **      Only allow for hexadecimal characters and convert
  60.                         **      lowercase to uppercase.
  61.                         **/
  62.                         if ( sgw->EditOp == EO_REPLACECHAR || sgw->EditOp == EO_INSERTCHAR ) {
  63.                                 if ( ! isxdigit( sgw->Code )) {
  64.                                         sgw->Actions |= SGA_BEEP;
  65.                                         sgw->Actions &= ~SGA_USE;
  66.                                 } else {
  67.                                         sgw->WorkBuffer[ sgw->BufferPos - 1 ] = toupper( sgw->Code );
  68.                                 }
  69.                         }
  70.                         break;
  71.  
  72.                 case    SGH_CLICK:
  73.                         /*
  74.                         **      Convert the character under the
  75.                         **      cursor to 0.
  76.                         **/
  77.                         if ( sgw->BufferPos < sgw->NumChars )
  78.                                 sgw->WorkBuffer[ sgw->BufferPos ] = '0';
  79.                         break;
  80.  
  81.                 default:
  82.                         rc = 0L;
  83.                         break;
  84.         }
  85.         return( rc );
  86. }
  87.  
  88. /*
  89. **      Uncomment the below typedef if your compiler
  90. **      complains about it.
  91. **/
  92.  
  93. /* typedef ULONG (*HOOKFUNC)(); */
  94.  
  95. struct Hook HexHook = { NULL, NULL, (HOOKFUNC)HexHookFunc, NULL, NULL };
  96.  
  97. VOID StartDemo( void )
  98. {
  99.     struct Window           *window;
  100.     Object                  *WO_Window, *GO_Quit;
  101.     ULONG                    signal, rc;
  102.     BOOL                     running = TRUE;
  103.  
  104.     /*
  105.     **      Create the window object.
  106.     **/
  107.     WO_Window = WindowObject,
  108.         WINDOW_Title,           "String Edit Hook Demo",
  109.         WINDOW_AutoAspect,      TRUE,
  110.         WINDOW_LockHeight,      TRUE,
  111.         WINDOW_RMBTrap,         TRUE,
  112.         WINDOW_AutoKeyLabel,        TRUE,
  113.         WINDOW_MasterGroup,
  114.             VGroupObject, NormalHOffset, NormalVOffset, NormalSpacing,
  115.                 GROUP_BackFill, FILL_RASTER,
  116.                 StartMember,
  117.                     InfoFixed( NULL, WinInfo, NULL, 5 ),
  118.                 EndMember,
  119.                 StartMember,
  120.                     HGroupObject, HOffset( 4 ), VOffset( 4 ), FRM_Type, FRTYPE_BUTTON, FRM_Recessed, TRUE,
  121.                         StartMember,
  122.                             StringObject,
  123.                                 LAB_Label,              "Only HEX characters:",
  124.                                 LAB_Style,              FSF_BOLD,
  125.                                 FuzzRidgeFrame,
  126.                                 STRINGA_MaxChars,       256,
  127.                                 STRINGA_EditHook,       &HexHook,
  128.                             EndObject,
  129.                         EndMember,
  130.                     EndObject,
  131.                 EndMember,
  132.                 StartMember,
  133.                     HGroupObject,
  134.                         VarSpace( 50 ),
  135.                             StartMember, GO_Quit  = FuzzButton( "_Quit",  ID_QUIT  ), EndMember,
  136.                         VarSpace( 50 ),
  137.                     EndObject, FixMinHeight,
  138.                 EndMember,
  139.             EndObject,
  140.         EndObject;
  141.  
  142.     /*
  143.     **      Object created OK?
  144.     **/
  145.     if ( WO_Window )
  146.     {
  147.         /*
  148.         **      try to open the window.
  149.         **/
  150.         if ( window = WindowOpen( WO_Window ))
  151.         {
  152.             /*
  153.             **      Obtain it's wait mask.
  154.             **/
  155.             GetAttr( WINDOW_SigMask, WO_Window, &signal );
  156.             /*
  157.             **      Event loop...
  158.             **/
  159.             do {
  160.                 Wait( signal );
  161.                 /*
  162.                 **      Handle events.
  163.                 **/
  164.                 while (( rc = HandleEvent( WO_Window )) != WMHI_NOMORE )
  165.                 {
  166.                     /*
  167.                     **      Evaluate return code.
  168.                     **/
  169.                     switch ( rc )
  170.                     {
  171.                     case WMHI_CLOSEWINDOW:
  172.                     case ID_QUIT:
  173.                         running = FALSE;
  174.                         break;
  175.                     }
  176.                 }
  177.             }    while ( running );
  178.         }    else Tell( "Could not open the window\n" );
  179.         /*
  180.         **      Disposing of the window object will
  181.         **      also close the window if it is
  182.         **      already opened and it will dispose of
  183.         **      all objects attached to it.
  184.         **/
  185.         DisposeObject( WO_Window );
  186.     }    else Tell( "Could not create the window object\n" );
  187. }
  188.